// pages/chuku/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    materialText:'',
    bar_code:'',
    materialId:'',
    id:'',
    mdId:'',
    num:'',
    type:'',
    warehouseText:'',
    warehouseValue:[],
    warehouseId:'',
    wText:'',
    wValue:[],
    wId:'',
    wareHouseData:[],
    loctarText:'',
    loctarValue:[],
    loctarId:'',
    loctarData:[],
    ywText:'',
    ywValue:[],
    ywId:'',
    ywData:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    
this.getWareHouse();
this.getYwData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  warehousePicker(){
    this.setData({ warehouseVisible: true});
  },
  wPicker(){
    this.setData({ wVisible: true});   
  },
  loctarPicker(){
    this.setData({  loctarVisible: true});
  },
  ywPicker(){
    this.setData({ywVisible:true})
  },
  selectWareHouse(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
    });
    console.log('data',this.data)
  },
  onPickerCancel(e){
    const { key } = e.currentTarget.dataset.key;
  
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  getWareHouse(){
    let that=this;
    app.common.http_post('/miniProgram/Other/getWarehouse',{},function(res){
          let data=res.data;
         that.setData({wareHouseData:data});
        })
  }, 
  burD(e){
    this.setData({bar_code:e.detail.value,materialId:'',materialText:''})
    this.getInfo();
  },
  selectMaterial(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
    });
    console.log('data',this.data)
    this.getInfo();
  },
  scanCode(){
    let that=this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
    console.log('scr',res)
        if (res.errMsg === "scanCode:ok") {
          let code=res.result;
        that.setData({bar_code:code,materialId:'',materialText:''})
        that.getInfo()
        }
      }
    })
  },
  getInfo(){
    let that=this;
    that.setData({detail:{}})
    let bar_code=that.data.bar_code;
    let materialId=that.data.materialId;
    let wId=that.data.wId;
    if(wId==''){
      app.common.alert('请选择仓库');
      return;
    }
    if(bar_code==''&&materialId==''){
      return;
    }
    
    let param={bar_code:bar_code,mdId:materialId,wId:wId};
    app.common.http_post('/miniProgram/Other/getKucunInfo',param,function(res){
  console.log('res',res)
      let data=res.data;
      console.log('wlData',data);
     that.setData({detail:data,mdId:data.mdId});
    })
  },
  getYwData(){
    let that=this;
    app.common.http_post('/miniProgram/Other/getywtype',{type:2},function(res){
      let data=res.data;
     that.setData({ywData:data});
    })
  },
  bindKeyInput: function (e) {
    this.setData({materialText:e.detail.value,bar_code:''})
    let that=this;
    let param={mdName:e.detail.value};
    console.log('ss',e.detail.value)
    if(e.detail.value==''){
      this.setData({mdId:'',materialVisible:false})
      return;
    }
    app.common.http_post('/miniProgram/Other/getMaterial',param,function(res){
      let data=res.data;
      console.log('wlData',data);
      that.setData({materialData:data})
      if(data.length>0&&e.detail.value!=''){
        that.setData({materialVisible:true})
      }
        
     
    })
    

  },
  setNum(e){
    this.setData({num:e.detail.value})
  },
  sureOut(){
    let that=this;
    let mdId=that.data.mdId;
    let outNum=that.data.num;
    let type=that.data.ywId;
    let wId=that.data.wId;
    if(mdId==''||outNum==''||type==''||wId==''){
       app.common.alert('参数缺失')
    return false;
    }
    let hasNum=that.data.detail.countNum-that.data.detail.sendNum;
    if(outNum<=0||outNum>hasNum){
      app.common.alert('参数有误')
      return false;
    }

    let param={
      mdId:mdId,
      wId:wId,
      num:outNum,
      type:type
    };
    app.common.showLoading();
    app.common.http_post('/miniProgram/OutReceipt/onSure',param,function(res){    
      app.common.hideLoading();
     app.common.alert(res.msg)
     setTimeout(function(){
       app.common.go_back();
     },500)
    })
  }
  
})